home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_375 / parm / menualloc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  224 lines

  1. /*
  2.  *    MenuAlloc.c - Copyright © 1990 by S.R. & P.C.
  3.  *
  4.  *    Created:    16 Jun 1990
  5.  *    Modified:    05 Jul 1990
  6.  *
  7.  *    Make>> make
  8.  */
  9.  
  10. /*
  11. #define DO_ARP_COPIES
  12. #include <exec/memory.h>
  13. #include <libraries/arpbase.h>
  14. #include <functions.h>
  15. */
  16.  
  17. #include "ParM.h"
  18.  
  19. #define STEP 10
  20.  
  21.  
  22. /*****                 global functions                    *****/
  23.  
  24. char *copystr(char *);
  25. void AddMenu(char *);
  26. void AddSubMenu(char *);
  27. void AddEntry(char *, char *, char*, char*, char, char, long, short);
  28. void EndSubMenu(void);
  29. void CleanUp(void);
  30. void FreeMenus(void);
  31.  
  32.  
  33. /*****                 global variables                    *****/
  34.  
  35. extern struct Menu Menu1;
  36. extern UBYTE menu_pen;
  37. extern struct TextAttr TOPAZ80;
  38.  
  39.  
  40. /*****                 local variables                    *****/
  41.  
  42. static struct Menu *CurrentMenu = &Menu1;
  43. static struct MenuItem *CurrentSubMenu, **CurrentItem;
  44.  
  45.  
  46. /* clean up widths and other info now that menu is all built */
  47.  
  48. void CleanUp(void)
  49. {
  50.     UWORD maxw, smaxw, txtw, top, stop, left;
  51.     struct Menu *mptr;
  52.     struct MenuItem *iptr, *sptr;
  53.  
  54.     left = Menu1.Width + STEP;
  55.     for( mptr = Menu1.NextMenu ; mptr ; mptr=mptr->NextMenu ) {
  56.         mptr->LeftEdge = left;
  57.         maxw = mptr->Width = (strlen(mptr->MenuName)+2) * 8;
  58.         left += maxw + STEP;
  59.         top = 0;
  60.         /* determine max width */
  61.         for( iptr = mptr->FirstItem ; iptr ; iptr=iptr->NextItem ) {
  62.             iptr->TopEdge = top;
  63.             top += iptr->Height;
  64.             txtw = IntuiTextLength((struct IntuiText *)iptr->ItemFill)+2;
  65.             if( iptr->Flags & COMMSEQ ) txtw += 48;
  66.             if( txtw > maxw ) maxw = txtw;
  67.         }
  68.         for( iptr = mptr->FirstItem ; iptr ; iptr=iptr->NextItem ) {
  69.             iptr->Width = maxw;
  70.             stop = smaxw = 0;
  71.             for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem ) {
  72.                 sptr->LeftEdge = maxw;
  73.                 sptr->TopEdge = stop;
  74.                 stop += sptr->Height;
  75.                 txtw = IntuiTextLength((struct IntuiText *)sptr->ItemFill)+2;
  76.                 if( sptr->Flags & COMMSEQ ) txtw += 48;
  77.                 if( txtw > smaxw ) smaxw = txtw;
  78.             }
  79.             for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem )
  80.                 sptr->Width = smaxw;
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86. /*****  make (and allocate) a copy of the passed string *****/
  87.  
  88. char *copystr( char *str )
  89. {
  90.     char *newstr;
  91.     newstr = AllocMem(strlen(str)+1, MEMF_PUBLIC);
  92.     strcpy(newstr, str);
  93.     return newstr;
  94. }
  95.  
  96. /* allocate and initialize a new MenuItem */
  97.  
  98.  
  99. struct Extended_MenuItem *AllocItem(char *itemstr)
  100. {
  101.     struct IntuiText *IT;
  102.     struct Extended_MenuItem *emi;
  103.  
  104.     emi = (struct Extended_MenuItem *)
  105.           AllocMem(sizeof(struct Extended_MenuItem), MEMF_PUBLIC|MEMF_CLEAR);
  106.     IT = (struct IntuiText *)AllocMem(sizeof(struct IntuiText), MEMF_PUBLIC|MEMF_CLEAR);
  107.     emi->emi_MenuItem.Height = 10;
  108.     emi->emi_MenuItem.Flags = ITEMTEXT+HIGHCOMP+ITEMENABLED;
  109.     IT->FrontPen = menu_pen;
  110.     IT->LeftEdge = IT->TopEdge = 1;
  111.     IT->ITextFont = &TOPAZ80;
  112.     IT->DrawMode = JAM1;
  113.     IT->IText = (UBYTE*)copystr(itemstr);
  114.     emi->emi_MenuItem.ItemFill = (APTR)IT;
  115.     return emi;
  116. }
  117.  
  118.  
  119. /* allocate and initialize a new Menu */
  120.  
  121. void AddMenu(char *str)
  122. {
  123.     struct Menu *Menu;
  124.  
  125.     Menu = (struct Menu *)AllocMem(sizeof(struct Menu), MEMF_PUBLIC|MEMF_CLEAR);
  126.     Menu->MenuName = copystr(str);
  127.     Menu->Flags = MENUENABLED;
  128.     CurrentMenu->NextMenu = Menu;
  129.     CurrentMenu = Menu;
  130.     CurrentItem = &Menu->FirstItem;
  131. }
  132.  
  133.  
  134. void AddSubMenu(char *substr)
  135. {
  136.     *CurrentItem = (struct MenuItem *)AllocItem(substr);
  137.     CurrentSubMenu = *CurrentItem;
  138.     CurrentItem = &CurrentSubMenu->SubItem;
  139. }
  140.  
  141.  
  142. void EndSubMenu(void)
  143. {
  144.     CurrentItem = &CurrentSubMenu->NextItem;
  145. }
  146.  
  147.  
  148. void AddEntry(    char *item,
  149.                 char *cmd,
  150.                 char *args,
  151.                 char *win,
  152.                 char shortcut,
  153.                 char mode,
  154.                 long stk,
  155.                 short pri )
  156. {
  157.     struct Extended_MenuItem *emi;
  158.  
  159.     emi = AllocItem(item);
  160.     emi->emi_Mode = mode;
  161.     emi->emi_Cmd = copystr(cmd);
  162.     if (args) emi->emi_Args = copystr(args);
  163.     if (shortcut) {
  164.         emi->emi_MenuItem.Flags |= COMMSEQ;
  165.         emi->emi_MenuItem.Command = shortcut;
  166.     }
  167.     if (mode=='c') {
  168.         emi->emi_Window = copystr(win);
  169.         emi->emi_Pri = pri;
  170.         if (stk < DEFAULT_STACK) stk = DEFAULT_STACK;
  171.         emi->emi_Stack = stk;
  172.     }
  173.     *CurrentItem = (struct MenuItem *)emi;
  174.     CurrentItem = &emi->emi_MenuItem.NextItem;
  175. }
  176.  
  177.  
  178. /* free up memory used by a menu item */
  179.  
  180. static void FreeItem( struct Extended_MenuItem *Item )
  181. {
  182.     struct IntuiText *IT;
  183.  
  184.     IT = (struct IntuiText *)Item->emi_MenuItem.ItemFill;
  185.     if (IT->IText) FreeMem(IT->IText, strlen((char *)IT->IText)+1);
  186.     if (IT) FreeMem(IT, sizeof(struct IntuiText));
  187.     if (Item->emi_Cmd) FreeMem(Item->emi_Cmd, strlen(Item->emi_Cmd)+1);
  188.     if (Item->emi_Args) FreeMem(Item->emi_Args, strlen(Item->emi_Args)+1);
  189.     if (Item->emi_Window) FreeMem(Item->emi_Window, strlen(Item->emi_Window)+1);
  190.     FreeMem(Item, sizeof(struct Extended_MenuItem));
  191. }
  192.  
  193.  
  194. /* free up all space taken up by our menus */
  195.  
  196. void FreeMenus( void )
  197. {
  198.     struct Menu *mptr, *mtmp;
  199.     struct MenuItem *iptr, *sptr, *itmp;
  200.  
  201.     mptr = Menu1.NextMenu;
  202.     while( mptr ) {
  203.         iptr = mptr->FirstItem;
  204.         while( iptr ) {
  205.             sptr = iptr->SubItem;
  206.             while( sptr ) {
  207.                 itmp = sptr;
  208.                 sptr = sptr->NextItem;
  209.                 FreeItem( (struct Extended_MenuItem *)itmp );
  210.             }
  211.             itmp = iptr;
  212.             iptr = iptr->NextItem;
  213.             FreeItem( (struct Extended_MenuItem *)itmp );
  214.         }
  215.         mtmp = mptr;
  216.         mptr = mptr->NextMenu;
  217.         if (mtmp->MenuName) FreeMem(mtmp->MenuName, strlen(mtmp->MenuName)+1);
  218.         FreeMem( mtmp , sizeof(struct Menu) );
  219.     }
  220.     Menu1.NextMenu = NULL;
  221.     CurrentMenu = &Menu1;
  222. }
  223.  
  224.